/**
 * Created by lupf on 2016/4/28.
 */

angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'dateFilter', 'myConstant', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                               CacheService, limitToFilter, filterFilter, dateFilter, myConstant) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};

            //缓存数据初始化(需要缓存的key请自定义)
            /*数据格式{
             key1 :value1,
             key2:value2
             }*/
            vm.cached = {
                RESERVE_LIST:{},
                RESERVE_INFO:{}
            };


            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            //获取对象数据[user,user,user]
            vm.getObj = function (key, input) {
                return $http.post(config.ctx + '/base/cache/getAll',
                    $httpParamSerializerJQLike({cacheKey: key}))
                    .then(function (response) {
                        return limitToFilter(filterFilter(response.data.object, input), 6);
                    }, function (response) {
                        $log.error('获取数据%s失败', cacheKey);
                    })
            };
            (function(){
            $http.post(config.ctx + "/reserve/bank/info/init/bank", {}, config.jsonHeader)
                .then(function (response) {
                    var data = response.data;
                    if (data.success) {
                            vm.cached.BANK_INFO = data.object;
                        var arr = [];
                            vm.cached.RESERVE_LIST = [];
                        for (var index in data.object) {
                            var item = data.object[index];
                            var bankInfo = {'accountNo': item.accountNo, 'desc': item.openBankName};
                            arr.push(bankInfo);
                                vm.cached.RESERVE_LIST.push({'accountNo':item.accountNo,'bankCode':item.remarkDesc});
                        }
                            vm.cached.BANK_INFO_LIST = arr;
                    } else {
                        alert(data.msg);
                    }
                    });
            }());
     /*       $('.datetimepicker').datetimepicker({
                language: 'zh-CN',
                todayBtn: true,
                //clearBtn:true,
                todayHighlight: true,
                initialDate: new Date(),
                format: 'yyyymmddhhii',
                forceParse: true,
                //format: 'yyyy-mm-dd hh:ii:ss',
                autoclose: true,
                minView: 0,
                minuteStep: 1
            });
*/

            vm.BANK_INFO_LIST={}
            $http.post(config.ctx + "/reserve/bank/info/init/bank",{}, config.jsonHeader)
                .then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        var arr = [];
                        for (var index in data.object){
                            var item = data.object[index];
                            var bankInfo ={'accountNo':item.accountNo,'desc':item.openBankName};
                            arr.push(bankInfo);
                        }
                        vm.BANK_INFO_LIST = arr;
                        console.log(arr);
                    } else {
                        alert(data.msg);
                    }
                }, function (response) {
                    $log.error('获取数据%s失败', '银行账号');
                })

            //数据格式化格式化
            vm.getBankInfo = function () {
                var arr = [];
                for (var indexTemp in vm.BANK_INFO_LIST) {
                    var itemTemp = vm.BANK_INFO_LIST[indexTemp];
                    arr.push({'key': itemTemp.accountNo, 'value':itemTemp.accountNo});
                }
                return arr;
            };
            $('.datetimepicker').datetimepicker({
                language:  'zh-CN',
                todayBtn: true,
                //clearBtn:true,
                todayHighlight:true,
                initialDate:new Date(),
                format:'yyyymmddhhii',
                forceParse: true,
                //format: 'yyyy-mm-dd hh:ii:ss',
                autoclose: true,
                minView: 0,
                minuteStep:1
            });

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;

            /*------------------以上配置通用---------------------*/


            vm.callbackMessage = callbackMessage;
            vm.resetNonSettlement = resetNonSettlement;
            vm.resetPayTrans = resetPayTrans;
            vm.resetReport = resetReport;
            vm.resetReportBase = resetReportBase;
            vm.resetDepositSettlement = resetDepositSettlement;
            vm.resetRemitSettlement = resetRemitSettlement;
            vm.initBankBalance = initBankBalance;

            //业务数据

            vm.resetBusiPayTrans = resetBusiPayTrans;
            vm.createBusiBacthNo = createBusiBacthNo;
            vm.dealReqPayTransDetail = dealReqPayTransDetail;
            vm.dealPayTransDetail = dealPayTransDetail;
            vm.monthReportCopy = monthReportCopy;

            /*------------------以上方法名可选择性通用---------------------*/

            /*CacheService.initCache(vm.cached, function (cacheKey, cacheObj) {
             $log.debug(cacheKey, cacheObj);
             vm.cached[cacheKey] = cacheObj;
             });*/

            /*vm.getCache = function (key) {
             CacheService.getCache(key)
             }*/

            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                var postData = angular.copy(vm.queryBean);
                postData.pageSize = vm.pagination.pageSize;
                postData.pageNum = vm.pagination.pageNum;
                $http.post(vm.url, $httpParamSerializerJQLike(postData))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            //清空选中记录
                            CheckboxService.clearChecked();
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function resetForm() {
                vm.queryBean = {};
            }

            function updateChecked($event, id) {
                CheckboxService.updateChecked($event, id);
                $log.debug(id);
                $log.debug(CheckboxService.getChecked());
            }

            function resetReportBase() {
                $http.post(config.ctx + '/reserve/reset/month/base',
                    {
                        'baseBusinessDateStart': dateFilter(vm.query.base.baseBusinessDateStart, 'yyyy-MM-dd'),
                        'baseBusinessDateEnd': dateFilter(vm.query.base.baseBusinessDateEnd, 'yyyy-MM-dd')
                    },
                    config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function resetReport() {
                $http.post(config.ctx + '/reserve/reset/month/report',
                    {
                        'reportBusinessDateStart': dateFilter(vm.query.base.reportBusinessDateStart, 'yyyy-MM-dd'),
                        'reportBusinessDateEnd': dateFilter(vm.query.base.reportBusinessDateEnd, 'yyyy-MM-dd')
                    },
                    config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function resetPayTrans() {
                $http.post(config.ctx + '/reserve/reset/pay/trans', {
                        'startDate': dateFilter(vm.query.base.startDate, 'yyyyMMddHHmm'),
                        'endDate': dateFilter(vm.query.base.endDate, 'yyyyMMddHHmm')
                    }, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function resetNonSettlement() {
                $http.post(config.ctx + '/reserve/reset/non/settlement', {
                        'NonBusinessDate': dateFilter(vm.query.base.NonBusinessDate, 'yyyy-MM-dd'),
                        'accountNo': vm.query.base.accountNo
                    }, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function resetDepositSettlement() {
                $http.post(config.ctx + '/reserve/reset/deposit/settlement', {'depositBusinessDate': dateFilter(vm.query.base.depositBusinessDate, 'yyyy-MM-dd')}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function resetRemitSettlement() {
                $http.post(config.ctx + '/reserve/reset/remit/settlement', {'remitBusinessDate': dateFilter(vm.query.base.remitBusinessDate, 'yyyy-MM-dd')}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function callbackMessage() {
                var message = vm.item.call.message;
                $http.post(config.ctx + '/reserve/reset/callback', {'message': vm.item.call.message}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }


            function resetBusiPayTrans() {
                $http.post(config.ctx + '/reserve/reset/pay/resetBusiPayTrans',
                    {'startTime': dateFilter(vm.query.base.startTime, 'yyyyMMddHHmm'), 'endTime': dateFilter(vm.query.base.endTime, 'yyyyMMddHHmm')}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function createBusiBacthNo() {
                $http.post(config.ctx + '/reserve/reset/pay/createBusiBacthNo',
                    {}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function dealReqPayTransDetail() {
                $http.post(config.ctx + '/reserve/reset/pay/dealReqPayTransDetail',
                    {}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function dealPayTransDetail() {
                $http.post(config.ctx + '/reserve/reset/pay/dealPayTransDetail',
                    {}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function initBankBalance() {
                $http.post(config.ctx + '/reserve/reset/bank/init/balance',
                    {
                        'accountNo': vm.query.base.accountNo,
                        'businessDate': dateFilter(vm.query.base.businessDate, 'yyyyMMdd')
                    }, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error:{}", response);
                    });
            }

            function monthReportCopy() {
                $http.post(config.ctx + '/reserve/reset/bank/init/copy',
                    {'businessDate': dateFilter(vm.query.copy.businessDate, 'yyyyMM')}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert("操作失败");
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error:{}", response);
                    });
            }

        }]);
